package com.a.lib_base.base.ui

import android.os.Bundle
import androidx.viewbinding.ViewBinding
import com.blankj.utilcode.util.LogUtils
import com.a.lib_base.R
import com.a.lib_base.base.viewmodel.BaseNetStateViewModel
import com.a.lib_network.bean.NetState
import com.a.lib_network.enums.NetStateEnum
import com.a.lib_utils.TipUtil.delayDismiss
import com.a.lib_utils.TipUtil.failTip
import com.a.lib_utils.TipUtil.loadingTip
import com.a.lib_utils.TipUtil.toast

/**
 * 含有ViewBinding 和 BaseNetStateViewModel 的Activity类
 * 用于根据状态进行处理，默认初始化了binding 和 viewModel
 * */
abstract class BaseNetStateActivity<VB : ViewBinding, VM : BaseNetStateViewModel> : BaseViewModelActivity<VB>() {

    lateinit var netStateViewModel: VM


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        /*viewModel 初始化*/
        initViewModel()
        netStateViewModel.owner = this
    }


    override fun initViewModel() {
        try {
            val clazz = com.a.lib_utils.ClassUtil.getGenericSuperclass(this.javaClass, 1) as Class<VM>
            viewModelAssist.getActivityViewModel(this, clazz)?.let {
                netStateViewModel = it
            }
        } catch (e: Exception) {
            LogUtils.e(e)
        }
    }

    fun simpleHandleState(state: NetState) {
        when (state.state) {
            NetStateEnum.CREATE_ORDER_SUCCESS -> toast(this, state.msg)
            NetStateEnum.CREATE_ORDER_FAIL -> toast(this, state.msg)
            NetStateEnum.NO_MORE -> toast(state.msg)
            NetStateEnum.NO_DATA -> toast(state.msg)
            NetStateEnum.FAIL -> failTip(this, state.msg)
            NetStateEnum.END -> delayDismiss(700)
            NetStateEnum.START -> loadingTip(this, getString(R.string.loading))
            else -> {}
        }
    }
}